package com.example.spring.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.spring.demo.common.error.CustomException;
import com.example.spring.demo.common.error.ErrorCodeEnum;
import com.example.spring.demo.common.model.req.MessageDTO;
import com.example.spring.demo.entity.Message;
import com.example.spring.demo.entity.Task;
import com.example.spring.demo.mapper.MessageMapper;
import com.example.spring.demo.service.MessageService;
import com.example.spring.demo.service.TaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.UUID;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/12/29 10:41
 **/
@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {

    private final MessageMapper messageMapper;
    private final TaskService taskService;

    @Override
    public Message getMessageById(String id) {
        Message message = messageMapper.getMessageById(id);
        if (Objects.isNull(message)) {
            throw new CustomException(ErrorCodeEnum.MESSAGE_IS_NULL);
        }
        return message;
    }

    @Override
    public Message getMessageByName(String name) {
        Message message = messageMapper.getMessageByName(name);
        if (Objects.isNull(message)) {
            throw new CustomException(ErrorCodeEnum.MESSAGE_IS_NULL);
        }
        return message;
    }

    @Override
    public void saveMessage(MessageDTO messageDTO) {
        Message message = BeanUtil.toBean(messageDTO, Message.class);
        int result = messageMapper.saveMessage(message);
        if(result == 0) {
            throw new CustomException(ErrorCodeEnum.MESSAGE_SAVE_FAIL);
        }
    }

    @Override
    public void saveMessage(Message message) {
        int result = messageMapper.saveMessage(message);
        if(result == 0) {
            throw new CustomException(ErrorCodeEnum.MESSAGE_SAVE_FAIL);
        }
    }

    @Override
    public void saveUserAddGoods(Integer userId, String goodsName) {
        String messageId = UUID.randomUUID().toString();
        String content;
        if(userId == -1) {
            content = "未知用户，新增商品：" + goodsName;
        }else {
            content = "用户：" + userId + "，新增商品：" + goodsName;
        }
        messageMapper.saveMessage(Message.builder()
                .id(messageId)
                .name("用户新增商品")
                .content(content)
                .build());
        taskService.saveMessageTaskToAdmin(messageId);
    }

    @Override
    public void saveUserDelGoods(Integer userId, Long goodsId) {
        String messageId = UUID.randomUUID().toString();
        String content;
        if(userId == -1) {
            content = "未知用户，删除商品id为：" + goodsId;
        }else {
            content = "用户：" + userId + "，删除商品id为：" + goodsId;
        }
        messageMapper.saveMessage(Message.builder()
                .id(messageId)
                .name("用户删除商品")
                .content(content)
                .build());
        taskService.saveMessageTaskToAdmin(messageId);
    }
}
